import { ref } from 'vue'
function useThrottle<T extends (...args: any[]) => any>(fn: T, interval: number) {
  const throttled = ref(false)
  let timer: NodeJS.Timeout | null = null
  const throttledFn: T = ((...args: any[]) => {
    if (!throttled.value) {
      throttled.value = true
      fn(...args)
      timer = setTimeout(() => {
        throttled.value = false
      }, interval)
    }
  }) as T
  throttledFn.cancel = () => {
    if (timer) {
      clearTimeout(timer)
      throttled.value = false
    }
  }
  return throttledFn
}
export default useThrottle